package com.hwua.tonyshop.controller.bg;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hwua.tonyshop.criteria.BannerCriteria;
import com.hwua.tonyshop.entity.mojo.BannerAO;
import com.hwua.tonyshop.entity.pojo.Banner;
import com.hwua.tonyshop.service.IBannerService;
import com.hwua.tonyshop.util.FileUtil;
import com.slkj.autocode.entity.ServiceResult;
import com.slkj.autocode.util.DateTimeUtils;
/**
 * 
 * @author sunny Yang
 * @date 2018年5月24日
 * @discribe
 * 轮播图管理
 *
 */
@Controller
@RequestMapping(value = "/back/banner")
public class BannerManagerController {
	@Resource
	IBannerService bannerService;
	/**
	 * 跳转到轮播图管理界面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toBannerManagerPage", method = { RequestMethod.GET, RequestMethod.POST })
	public String toBannerManagerPage(Model model,HttpServletRequest request,HttpServletResponse response) {
		return "background/oz_bannerImg";
	}
	
	@RequestMapping(value = "/getBanner", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<BannerAO> getBanner(Integer bannerId,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return bannerService.selectById(bannerId);
	}
	
	@RequestMapping(value = "/getBanners", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<List<BannerAO>> getBanners(Model model,HttpServletRequest request,HttpServletResponse response) {
		BannerCriteria criteria = new BannerCriteria();
		return bannerService.selectByCriteria(criteria);
	}
	
	@RequestMapping(value = "/deleteBanner", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> deleteBanner(Integer menuId,Model model,HttpServletRequest request,HttpServletResponse response) {
		return bannerService.deleteById(menuId);
	}
	
	@RequestMapping(value = "/addBanner", method = { RequestMethod.GET, RequestMethod.POST })
	public String addBanner(Banner banner,@RequestParam("file") MultipartFile file,Model model,HttpServletRequest request,HttpServletResponse response) {
		String fileName=file.getOriginalFilename();
		String newfileName = System.currentTimeMillis()+"";
		String path = request.getSession().getServletContext().getRealPath("static")+"/background/img/banner/"+newfileName+fileName.substring(fileName.lastIndexOf("."));
		if(FileUtil.uploadFile(file, path)){
			banner.setCreateDate(DateTimeUtils.formateDateToStr(new Date(), DateTimeUtils.FORMAT_YMD_HMS));
			banner.setImgSrc("background/img/banner/"+newfileName+fileName.substring(fileName.lastIndexOf(".")));
		}
		banner.setState("0");
		bannerService.addBanner(banner);
		return "forward:../../back/banner/toBannerManagerPage";
	}
	
	@RequestMapping(value = "/updateBanner", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> updateBanner(Banner banner,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return bannerService.updateById(banner);
	}
}
